package d.n.d.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import d.n.d.constant.Constant;
import d.n.d.domain.User;
import d.n.d.service.UserService;

/**
 * 
 * @author D.N.D
 * 
 */
@Controller
public class LoginController {

	@Autowired
	private UserService userService;

	/**
	 * <br> Call login page. <br/>
	 * 
	 * @param request
	 * @return login page
	 */
	@RequestMapping(value = "/tologin", method = RequestMethod.GET)
	public String sentoLoginPage(HttpServletRequest request) {

		return "login";
	}

	/**
	 * <br> Call support page for user support. <br/>
	 * 
	 * @param request
	 * @return search account page.
	 */
	@RequestMapping(value = "/SupportPage", method = RequestMethod.POST)
	public String sendtoSupportPage(HttpServletRequest request) {

		return "searchAccount";
	}

	/**
	 * <br> Call view transaction page method. <br/>
	 * 
	 * @param request
	 * @return view transaction page.
	 */
	@RequestMapping(value = "/ViewTransaction", method = RequestMethod.GET)
	public String sendtoViewTransaction(HttpServletRequest request) {

		return "viewTransaction";
	}

	/**
	 * <br> Call admin page method. <br/>
	 * 
	 * @param request
	 * @return return to review transaction page.
	 */
	@RequestMapping(value = "/AdminPage", method = RequestMethod.POST)
	public String sendtoAdminPage(HttpServletRequest request) {

		return "redirect:/adminReviewTransaction.html";
	}

	/**
	 * <br> Call home page. <br/>
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return return to home page.
	 */
	@RequestMapping(value = "/Home", method = RequestMethod.POST)
	public String checkLoginPage(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		String message = "";
		HttpSession session = request.getSession();
		String username = request.getParameter("userid");
		String password = request.getParameter("password");
		User user = userService.verifyLogin(username, password);
		if (user != null) {
			session.setAttribute("user", user);
			if (user.getUserRole().equals(Constant.USER_SUPPORT)) {
				session.setAttribute("usertype", Constant.USER_SUPPORT);
				return "forward:/SupportPage";
			} else if (user.getUserRole().equals(Constant.USER_ADMIN)) {
				session.setAttribute("usertype", Constant.USER_ADMIN);
				return "forward:/AdminPage";
			}
		} else {
			message = "Username or Password is wrong!";
			model.addAttribute("accountError", message);
		}
		return "login";
	}


	/**
	 * <br> Call logout action. <br>
	 * 
	 * @param request
	 * @return return to login page.
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logOutMethod(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		session.invalidate();
		return "redirect:/";
	}
}
